package com.erlinyou.im.service.goodfirend.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.erlinyou.base.service.BaseServiceImpl;
import com.erlinyou.im.dao.repository.chat.ChatMessageRepository;
import com.erlinyou.im.dao.repository.chat.ChatSessionRepository;
import com.erlinyou.im.dao.repository.goodfirend.GoodfirendRepository;
import com.erlinyou.im.model.chat.ChatSession;
import com.erlinyou.im.model.goodfirend.Goodfirend;
import com.erlinyou.im.model.goodfirend.GoodfirendKey;
import com.erlinyou.im.service.goodfirend.GoodfirendService;
@Service
public class GoodfirendServiceImpl extends BaseServiceImpl<Goodfirend, GoodfirendKey> implements GoodfirendService{

	@Resource
	private GoodfirendRepository goodfirendRepository;
	
	@Resource
	private ChatMessageRepository chatMessageRepository;
	
	@Resource
	private ChatSessionRepository chatSessionRepository;

	@Override
	public Goodfirend findByUseridAndJid(Long userid, Long jid) {
		return goodfirendRepository.findByUseridAndJid(userid,jid);
	}
	
	@CacheEvict(value="cache2",allEntries=true)
	@Override
	public <S extends Goodfirend> List<S> saveAll(Iterable<S> entities) {
		return goodfirendRepository.saveAll(entities);
	}
	
	@CacheEvict(value="cache2",allEntries=true)   //清空cache1 cache2的缓存
	@Transactional
	@Override
	public boolean delGoodFriend(Long userId, long rid,String gid) {
		int num = goodfirendRepository.deleteByUseridAndJid(userId,rid);
		if(num>0) {
			int num1 = goodfirendRepository.deleteByUseridAndJid(rid,userId);
			if(num1>0) {
				List<ChatSession> list = chatSessionRepository.findByUseridAndRidAndCtype(userId,rid,1);
				if(list.size()>0) {
					chatSessionRepository.deleteByUseridAndRidAndCtype(userId,rid,1);
					chatSessionRepository.deleteByUseridAndRidAndCtype(rid,userId,1);
					chatMessageRepository.deleteByGid(gid);
					return true;
				}else {
					return true;
				}
			}else {
				throw new RuntimeException();
			}
		}else {
			throw new RuntimeException();
		}
	}
	@Override
	@Cacheable(value="cache2",key="#root.methodName+':'+#userid+':'+#jid+':'+#relation") //缓存加入cache2中
	public Goodfirend findByUseridAndJidAndRelation(Long userid, long jid, int relation) {
		return goodfirendRepository.findByUseridAndJidAndRelation(userid,jid,relation);
	}

	@Override
	@Cacheable(value="cache2",key="#root.methodName+':'+#userid") //缓存加入cache2中
	public List<Goodfirend> findByUserid(Long userid) {
		return goodfirendRepository.findByUserid(userid);
	}

	@Override
	public List<Map<String, Object>> findByUseridAndRelation(Long userid) {
		return goodfirendRepository.findByUseridAndRelation(userid);
	}
/*
	@Override
	public Map<Integer, Map<String, String>> selectRoster(Long userId) {
		Map<String, String> map = goodfirendRepository.selectRoster(userId);
		
		Map<Integer,Map<String, String>> resultMap = new HashMap<>();
		
		String jid = map.get("jid").toString();
		String sub = map.get("sub").toString();
		String ask = map.get("ask").toString();
		String recv = map.get("recv").toString();
		String permission = map.get("permission").toString();
		String isCanChat = map.get("isCanChat").toString();
		String isCanSeePos = map.get("isCanSeePos").toString();
		String isCanSeeExperience = map.get("isCanSeeExperience").toString();
		String isSeeOtherExperience = map.get("isSeeOtherExperience").toString();
		String isSeeMapPhoto = map.get("isSeeMapPhoto").toString();
		String isAllowContactsGetPosition = map.get("isAllowContactsGetPosition").toString();
		
		Map<String, String> info=new HashMap<>();
		info.put("jid", jid);
		info.put("sub", sub);
		info.put("ask", ask);
		info.put("recv", recv);
		info.put("permission", permission);
		info.put("isCanChat", isCanChat);
		info.put("isCanSeePos", isCanSeePos);
		info.put("isCanSeeExperience", isCanSeeExperience);
		info.put("isSeeOtherExperience", isSeeOtherExperience);
		info.put("isSeeMapPhoto", isSeeMapPhoto);
		info.put("isAllowContactsGetPosition", isAllowContactsGetPosition);
		resultMap.put(Integer.valueOf(jid), info);
		
		return null;
	}
*/

	@Override
	public List<Integer> selectRoster(Long userId) {
		List<Integer> resultlist = new ArrayList<>();
	
		List<Map<String,Object>> list = goodfirendRepository.findByUseridAndRelation(userId);
		
		for (Map<String, Object> map : list) {
			Integer jid = Integer.valueOf(map.get("jid").toString());
			resultlist.add(jid);
		}
		resultlist.add(userId.intValue());
		
		return resultlist;
	}
	@Override
	public List<Integer> selectRoster1(Long userId) {
		List<Integer> resultlist = new ArrayList<>();
		
		List<Goodfirend> findByUserid = goodfirendRepository.findByUserid(userId);
		
		for (Goodfirend goodfirend : findByUserid) {
			Integer jid = (int) goodfirend.getJid();
			resultlist.add(jid);
		}
		resultlist.add(userId.intValue());
		
		return resultlist;
	}

	@Override
	public List<Integer> queryUserid(Long userId) {
		List<Map<String, Object>> queryUserid = goodfirendRepository.queryUserid(userId);
		List<Integer> list = new ArrayList<Integer>();
		for (Map<String, Object> map : queryUserid) {
			list.add(Integer.valueOf(String.valueOf(map.get("jid"))));
		}
		return list;
	}
	
}
